﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ClipperBaseUI.Controls;
using Clipper.BLL;
using System.Collections.ObjectModel;

namespace Clipper.DataManage
{
    /// <summary>
    /// WinDetectItemsInput.xaml 的交互逻辑
    /// </summary>
    public partial class WinDetectItemsInput : BaseWindow
    {
        public WinDetectItemsInput()
        {
            InitializeComponent();
        }

        private void BaseWindow_Loaded(object sender, RoutedEventArgs e)
        {
            BLL.DetectVariety detectvariety = new DetectVariety();
            DetectVarietyID.ItemsSource = detectvariety.GetModelList("");


           BLL.Reagent reagent = new Reagent();
            ObservableCollection < Model.Reagent>  reagentList= reagent.GetModelList("");
            reagentName1.ItemsSource = reagentList;
            reagentName2.ItemsSource = reagentList;
            reagentName3.ItemsSource = reagentList;
            //BLL.FittingApproachInfo fittingapproachInfo = new FittingApproachInfo();
            //FittingApproachID.ItemsSource = fittingapproachInfo.GetModelList("");

            //BLL.CoordinateInfo coordinateinfo = new CoordinateInfo();
            //CoordinateID.ItemsSource = coordinateinfo.GetModelList("");

            //BLL.StandardState standardState1 = new StandardState();
            //standardState.ItemsSource = standardState1.GetModelList("");

            if (BaseServiceProvider.InputName == Model.InputEnum.Update)
            {
                Model.DetectItems model = BaseServiceProvider.DataModel as Model.DetectItems;
                if (model != null)
                {
                    detectItem.IsEnabled = false;
                    DataContext = model;
                }
            }
        }

        private void Save_Click(object sender, RoutedEventArgs e)
        {
            bool result = true;
            if (Check())
            {
                Clipper.Model.DetectItems model = new Clipper.Model.DetectItems();
                Clipper.BLL.DetectItems d = new BLL.DetectItems();
                model.detectItem = detectItem.Text;
                model.name = name.Text;
                model.DetectVarietyID = Convert.ToInt32(DetectVarietyID.SelectedValue);
                model.bloodAmount = bloodAmount.Number;
                if (IncubationTime1.Text.Equals(""))
                {
                    model.incubationTime = 0;
                }
                else
                {
                model.incubationTime = byte.Parse(IncubationTime1.Text);
                }
                if (IncubationTime2.Text.Equals(""))
                {
                    model.incubationTime1 = 0;
                }
                else
                {

                model.incubationTime1 = byte.Parse(IncubationTime2.Text);
                }
                if (IncubationTime3.Text.Equals(""))
                {
                    model.incubationTime2 = 0;
                }
                else
                {
                model.incubationTime2 = byte.Parse(IncubationTime3.Text);
                }
                if (reagentName1.SelectedValue==null)
                {
                    model.reagentName1 = "";
                }
                else
                {
                model.reagentName1 = reagentName1.SelectedValue.ToString();

                }
                if (reagentAmount1.Text.Equals(""))
                {
                    model.reagentAmount1 = 0;
                }
                else
                {
                model.reagentAmount1 = byte.Parse(reagentAmount1.Text);
                }               
               
                model.reagentName2 = reagentName2.SelectedValue.ToString();
                model.reagentAmount2 = byte.Parse(reagentAmount2.Text);

                if (reagentName3.SelectedValue == null)
                {
                    model.reagentName3 = "";
                }
                else
                {
                  model.reagentName3 = reagentName3.SelectedValue.ToString();
                }

                if (reagentAmount3.Text.Equals(""))
                {
                    model.reagentAmount3 = 0;
                }
                else
                {
                    model.reagentAmount3 = byte.Parse(reagentAmount3.Text);
                }
                //if (incubationTime3.Text.Equals(""))
                //{
                //    model.incubationTime = 0;
                //}
                //else
                //{
                //    model.incubationTime = byte.Parse(incubationTime3.Text);
                //}

              

                model.detectTime = detectTime.Number;

                model.standardAmount = 0;
                model.FittingApproachID = 0;
                model.CoordinateID = 0;
                model.standardState =0;
                model.correlationThreshold =0;
                model.note = "";
                if (BaseServiceProvider.InputName == Model.InputEnum.Add)
                {
                    result=d.Add(model);
                }
                else
                {
                   result= d.Update(model);
                }
                this.DialogResult = result;
            }
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = null;
            this.Close();
        }
        private bool Check()
        {
            List<string> strErr = new List<string>();
            //if (string.IsNullOrEmpty(detectItem.Text))
            //{
            //    strErr.Add("DetectItemsNotNullTip");   //检测项名称不能为空！\n
            //}

            //if (string.IsNullOrEmpty(name.Text))
            //{
            //    strErr.Add("NameNotNullTip");//"全称不能为空！\n"
            //}

            if (DetectVarietyID.SelectedIndex < 0)
            {
                strErr.Add("DetectVarietyIDNotNullTip");//项目型不能为空！\n
            }

            if (string.IsNullOrEmpty(bloodAmount.Number.ToString()))
            {
                strErr.Add("BloodAmountNotNullTip");//样品用量不能为空！\n
            }

            //if (string.IsNullOrEmpty(reagentAmount1.Number.ToString()))
            //{
            //    strErr.Add("ReagentAmount1NotNullTip"); //试剂1用量不能为空！\n  
            //}

            if (string.IsNullOrEmpty(reagentAmount2.Number.ToString()))
            {
                strErr.Add("ReagentAmount2NotNullTip");//试剂2用量不能为空！\n
            }

            //if (string.IsNullOrEmpty(incubationTime.Number.ToString()))
            //{
            //    strErr.Add("IncubationTimeNotNullTip");//孵育时间不能为空！\n
            //}

            //if (string.IsNullOrEmpty(standardAmount.Number.ToString()))
            //{
            //    strErr.Add("StandardAmountNotNullTip");//"标准品用量不能为空！\n"
            //}

            //if (FittingApproachID.SelectedIndex < 0)
            //{
            //    strErr.Add("FittingApproachIDNotNullTip");//拟合方程不能为空！\n
            //}

            //if (CoordinateID.SelectedIndex < 0)
            //{
            //    strErr.Add("CoordinateIDNotNullTip");//坐标类型不能为空！\n
            //}

            //if (standardState.SelectedIndex < 0)
            //{
            //    strErr.Add("StandardStateNotNullTip");//标定状态不能为空！\n
            //}

            //if (string.IsNullOrEmpty(correlationThreshold.Text))
            //{
            //    strErr.Add("CorrelationThresholdNotNullTip");//"相关系数阈值不能为空！\n"
            //}

            if (strErr.Count>0)
            {
                UIHelper.ShowKeyMessage(strErr);
                return false;
            }
            return true;
        }

    }
}

